/**
 * 
 */
package com.door.erp.controller;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.door.erp.dwz.DwzResponse;
import com.door.erp.model.Client;
import com.door.erp.model.Product;
import com.door.erp.pager.Pager;
import com.door.erp.sevice.ClientService;

/**
 * @author xiaolongwei@gmail.com
 *
 */
@Controller
@RequestMapping(value = "/client")
public class ClientController {
	@Resource(name="clientService", type=ClientService.class)
	private ClientService clientService;
	@RequestMapping(value = "/save.do")
	public ModelAndView save(Client client) {
		client.setPwd(clientService.generatePwd());
		clientService.add(client);
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("client");
		ModelAndView mv = new ModelAndView("dwzresponse");	
		mv.addObject("response", resonse);
		return mv;
	}
	@RequestMapping(value = "/edit/{id}.do")
	public  ModelAndView  edit(@PathVariable(value="id") Long id ) {
		ModelAndView mv = new ModelAndView("client/edit");
		Client client = clientService.getObject(id);
		mv.addObject("client", client);
		return mv;
	}
	@RequestMapping(value = "/add.do")
	public ModelAndView add() {
		ModelAndView mv = new ModelAndView("client/add");	
		return mv;
	}
	@RequestMapping(value = "/index.do")
	public ModelAndView index(Pager pager) {
		ModelAndView mv = new ModelAndView("client/clientIndex");	
		List<Client> clients = clientService.getAll(new HashMap(), pager);
		mv.addObject("clients", clients);
		mv.addObject("pager", pager);
		return mv;
	}
	@RequestMapping(value = "/update.do")
	public ModelAndView update(Client client ) {
		Client c = clientService.getObject(client.getId());
		client.setPwd(c.getPwd());
		clientService.modify(client);
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("client");
		ModelAndView mv = new ModelAndView("dwzresponse");	
		mv.addObject("response", resonse);
		return mv;
	}
	@RequestMapping(value = "/delete/{id}.do")
	public ModelAndView delete(@PathVariable(value="id") Long id ) {
		ModelAndView mv = new ModelAndView("dwzresponse");
		clientService.delete(id);
	//	mv.addObject("stock", StockManager.pop());
		DwzResponse resonse = new DwzResponse();		
		resonse.setNavTabId("client");	
		mv.addObject("response", resonse);
		return mv;
	}
	@RequestMapping(value = "/find/{id}.do")
	public ModelAndView find(@PathVariable(value="id") Long id) {
		ModelAndView mv = new ModelAndView("index/index");
	//	mv.addObject("stock", StockManager.pop());
		return mv;
	}
}
